/**
 * A palette that allows you to create BPMN _and_ custom elements.
 */
import { customShapeAction, customFlowAction, batchCreateCustom } from "../../utils/util";

export default function PaletteProvider(palette, create, elementFactory, handTool, lassoTool, spaceTool, globalConnect, translate) {
	this.create = create;
	this.elementFactory = elementFactory;
	this.handTool = handTool;
	this.lassoTool = lassoTool;
	this.spaceTool = spaceTool;
	this.globalConnect = globalConnect;
	this.translate = translate;

	palette.registerProvider(this);
}

/* 貌似webpack打包才需要 */
PaletteProvider.$inject = ["palette", "create", "elementFactory", "handTool", "lassoTool", "spaceTool", "globalConnect", "translate"];

PaletteProvider.prototype.getPaletteEntries = function (element) {
	let actions = {};
	const { create, elementFactory, globalConnect, handTool } = this;
	// function createTask() {
	//     return function (event) {
	//         const shape = elementFactory.createShape({
	//             type: 'bpmn:Task'
	//         });
	//         console.log(shape) // 只在拖动或者点击时触发
	//         create.start(event, shape);
	//     }
	// }
	// return {
	//     'create.lindaidai-task': {
	//         group: 'model',
	//         className: 'icon-custom lindaidai-task',
	//         title: '创建一个类型为lindaidai-task的任务节点',
	//         action: {
	//             dragstart: createTask(),
	//             click: createTask()
	//         }
	//     }
	// }
	function createConnect(type, group, className, title, options) {
		return {
			group,
			className,
			title: "新增" + title,
			action: {
				click: function (event) {
					globalConnect.toggle(event);
				},
			},
		};
	}
	// 'global-connect-tool': {
	//     group: 'tools',
	//     className: 'bpmn-icon-connection-multi',//默认
	//     title: '新增线',
	//     action: {
	//       click: function (event) {
	//         globalConnect.toggle(event)
	//       }
	//     }
	//   }

	function createAction(type, group, className, title, options) {
		function createListener(event) {
			let shape = elementFactory.createShape(Object.assign({ type }, options));
			create.start(event, shape);
		}

		return {
			group,
			className,
			title: "新增" + title,
			action: {
				dragstart: createListener,
				click: createListener,
			},
		};
	}

	Object.assign(actions, {
		...batchCreateCustom(customFlowAction, createConnect), // 线
		...batchCreateCustom(customShapeAction, createAction),
	});
	console.log(actions);
	return actions;
};
